<?php
namespace Home\Controller;
use Think\Controller;
class AlbumController extends CommonController{
	public function index(){
		$sql = "select a.id,a.name,p.new_name as p_nam, p.path from oa_album a LEFT JOIN oa_album_picture p on a.id=p.album_id group by a.id";
		$data = M()->query($sql);
		foreach ($data as $key=>$value){
			$list[$key]['id'] = $value['id'];
			$list[$key]['name'] = $value['name'];
			if( empty($value['p_nam']) ){
				$list[$key]['img'] = __ROOT__."/Public/images/pic-none.png";
			}else{
				$list[$key]['img'] = $value['path']."/small/".$value['p_nam'];
			}
			
		}
		$this->assign("list", $list);
		$this->display();
	}
	
	Public function lists(){
		$sql = "select a.id,a.name,p.new_name as p_nam, p.path from oa_album a LEFT JOIN oa_album_picture p on a.id=p.album_id group by a.id";
		$data = M()->query($sql);
		foreach ($data as $key=>$value){
			$list[$key]['id'] = $value['id'];
			$list[$key]['name'] = $value['name'];
			if( empty($value['p_nam']) ){
				$list[$key]['img'] = __ROOT__."/Public/images/pic-none.png";
			}else{
				$list[$key]['img'] = $value['path']."/small/".$value['p_nam'];
			}
			
		}
		$this->assign("list", $list);
		$this->display();
	}
	
	public function add(){
		$name = I('post.name');
		if(!is_space($name) || empty($name)){$this->error("名称不能为空且不能含有空格！");}
		$model = M("album");
		$foldername = date("YmdHis");
		$data = array(
			'name' => $name,
			'foldername' => $foldername,
			'createtime' => date("Y-m-d H:i:s")
		);
		$path = $_SERVER['DOCUMENT_ROOT'].'/oa/Pictures/'.$foldername;
		if (!is_dir($path)){
			mkdir($path.'/big', '0777', 'recursive');
			mkdir($path.'/small', '0777', 'recursive');
		}
		$success = $model->data($data)->add();
		if($success){
			$this->systemlog("add", "相册管理", "添加新相册---".$name);
			$this->success("添加相册成功！","lists");
		}else{
			$this->success("添加相册失败！");
		}
	}
	
	public function edit(){
		$name = I("get.name");
		$id = I("get.id");
		if(!is_space($name) || empty($name)){$this->ajaxReturn("相册名称不能为空且不能含有空格！");}
		if(!is_id($id)){$this->ajaxReturn("非法参数");}
		M("album")->where("id=%d",$id)->setField("name",$name);
	}
	
	public function view(){
		$id = I("get.id");
		if(!is_id($id)){$this->error("非法参数！");}
		$model = M("album_picture");
		$name = M("album")->where("id=%d",$id)->getField("name");
		$list = $model->where("album_id=%d",$id)->select();
		$this->assign("list",$list);
		$this->assign("id",$id);
		$this->assign("name",$name);
		$this->display();
	}
	
	public function deletepic(){
		if( !in_array( "84", session("menu") ) ){
			$this->ajaxReturn("对不起你无权限删除照片！");
		}else{
			$id = trim( I("get.id"), ',' );
			$type = trim( I("get.type"), ',' );
			if(empty($id)){
				$this->ajaxReturn("请至少选择一张图片！");
			}else{
				$id_arr = explode(",", $id);
				$model = M("album_picture");
				$appname = C("APP_NAME");
				$root_path = $_SERVER['DOCUMENT_ROOT'];
				$foldername = M("album")->where("id=%d",$type)->getField("foldername");
				
				if( !empty( $appname ) ){
					$path = $root_path.'/'.C("APP_NAME").'/Pictures/'.$foldername.'/big/';
					$path_thumb = $root_path.'/'.C("APP_NAME").'/Pictures/'.$foldername.'/small/';//缩略图路径
				}else{
					$path = $root_path.'/Pictures/'.$foldername.'/big/';
					$path_thumb = $root_path.'/Pictures/'.$foldername.'/small/';
				}
				foreach ($id_arr as $value){
					$new_name = $model->where("id=%d",$value)->getField("new_name");
					unlink($path.$new_name);unlink($path_thumb.$new_name);
					$model->where("id=%d",$value)->delete();
					$this->systemlog("delete", "图片管理", "删除图片---".$new_name);
				}
				$where = substr($where, 0, -4);
				$this->ajaxReturn("图片删除成功！");
			}
		}
	}
	
	public function picture(){
		$id = I("get.id");
		if(!is_id($id)){$this->error("非法参数！");}
		$model = M("album_picture");
		$name = M("album")->where("id=%d",$id)->getField("name");
		$list = $model->where("album_id=%d",$id)->select();
		$this->assign("list",$list);
		$this->assign("id",$id);
		$this->assign("name",$name);
		$this->display();
	}
	
	public function show(){
		$id = I("get.id");
		if(!is_id($id)){$this->error("非法参数！");}
		$model = M("album_picture");
		$data = M("album")->where("id=%d",$id)->find();
		$list = $model->where("album_id=%d",$id)->select();
		$this->assign("list",$list);
		$this->assign("data",$data);
		$this->display();
	}
	
	public function upload(){
		$id = I("get.id");
		if(!is_id($id)){$this->error("非法参数！");}
		$data = M("album")->select();
		$this->assign("id",$id);
		$this->assign("data",$data);
		$this->display();
	}
	
	public function thumb($file_path,$file_path_thumb){
		$image = new \Think\Image(); 
		$image->open($file_path);
		$image->thumb(150, 150)->save($file_path_thumb);
	}
	
	public function uploadfile(){
		$time = date("Y-m-d H:i:s");
		$id = $this->getPost("album");
		$foldername = M("album")->where("id=%d",$id)->getField("foldername");
		// 注意：使用组件上传，不可以使用 $_FILES["Filedata"]["type"] 来判断文件类型
		mb_http_input("utf-8");
		mb_http_output("utf-8");
		
		//---------------------------------------------------------------------------------------------
		//组件设置a.MD5File为2，3时 的实例代码
		
		if($this->getGet('access2008_cmd')=='2'){ // 提交MD5验证后的文件信息进行验证
			die('0'); //返回命令  0 = 开始上传文件， 2 = 不上传文件，前台直接显示上传完成
		}
		if($this->getGet('access2008_cmd')=='3'){ //提交文件信息进行验证
			die('1'); //返回命令 0 = 开始上传文件,1 = 提交MD5验证后的文件信息进行验证, 2 = 不上传文件，前台直接显示上传完成
		}
		//---------------------------------------------------------------------------------------------
		$appname = C("APP_NAME");
		$root_path = $_SERVER['DOCUMENT_ROOT'];
		if( !empty( $appname ) ){
			$save_path = $root_path.'/'.C("APP_NAME").'/Pictures/'.$foldername.'/big/';
			$save_path_thumb = $root_path.'/'.C("APP_NAME").'/Pictures/'.$foldername.'/small/';//缩略图路径
		}else{
			$save_path = $root_path.'/Pictures/'.$foldername.'/big/';
			$save_path_thumb = $root_path.'/Pictures/'.$foldername.'/small/';
		}
		$save_url = C("PATH_URL").'/Pictures/'.$foldername;
		$ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
		$max_size = 1024*4000;//4M大小文件
		
//		$save_path = realpath($save_path) . '/';
		
		//有上传文件时
		if (empty($_FILES) === false) {
			//原文件名
			$file_name = $_FILES['Filedata']['name'];
			//服务器上临时文件名
			$tmp_name = $_FILES['Filedata']['tmp_name'];
			//文件大小
			$file_size = $_FILES['Filedata']['size'];
			//检查文件名
			if (!$file_name) {
				exit("返回错误: 请选择文件。");
			}
			//检查目录
			if (@is_dir($save_path) === false) {
				exit("返回错误: 上传目录不存在。($save_path)");
			}
			//检查目录写权限
			if (@is_writable($save_path) === false) {
				exit("返回错误: 上传目录没有写权限。($save_url)");
			}
			//检查是否已上传
			if (@is_uploaded_file($tmp_name) === false) {
				exit("返回错误: 临时文件可能不是上传文件。($file_name)");
			}
			//检查文件大小
			if ($file_size > $max_size) {
				exit("返回错误: 上传文件($file_name)大小超过限制。最大".($max_size/1024)."KB");
			}
			$temp_arr = explode(".", $file_name);
			$file_ext = array_pop($temp_arr);
			$file_ext = trim($file_ext);
			$file_ext = strtolower($file_ext);
			if (in_array($file_ext, $ext_arr) === false) {
					exit("返回错误: 上传文件扩展名是不允许的扩展名。");
			}
		    
			//新文件名
			$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
			//移动文件
			$file_path = $save_path . $new_file_name;
			$file_path_thumb = $save_path_thumb . $new_file_name;
			@chmod($file_path, 0644);//修改目录权限(Linux)
			if (move_uploaded_file($tmp_name, $file_path) === false) {//开始移动
				exit("返回错误: 上传文件失败。($file_name)");
			}
			$file_url = $save_url . $new_file_name;
			$this->thumb($file_path, $file_path_thumb);//生成缩略图
			$picture = M('album_picture');
			$data = array(
				'new_name' => $new_file_name,
				'path' => $save_url,
				'album_id' => $id,
				'time' => $time,
				'user_name' => session("user_name"),
				'old_name' => $file_name,
			);
			$picture->add($data);
		}
	}
	
	function filekzm($a)
	{
		$c=strrchr($a,'.');
		if($c)
		{
			return $c;
		}else{
			return '';
		}
	}
	
	function getGet($v)// 获取GET
	{
	  if(isset($_GET[$v]))
	  {
		 return $_GET[$v];
	  }else{
		 return '';
	  }
	}
	
	function getPost($v)// 获取POST
	{
	  if(isset($_POST[$v]))
	  {
		  return $_POST[$v];
	  }else{
		  return '';
	  }
	}
}
?>